package com.tiancheng.trade.order.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.enums.SettleStatusEnum;
import com.tiancheng.trade.commom.core.exception.Asserts;
import com.tiancheng.trade.commom.core.utils.AmountUtils;
import com.tiancheng.trade.order.service.BillService;
import com.tiancheng.trade.commom.core.utils.DateTimeUtil;
import com.tiancheng.trade.order.vo.admin.QueryBillReqVO;
import com.tiancheng.trade.order.vo.admin.TradeStatisticsResVO;
import com.tiancheng.trade.order.vo.admin.res.BillQueryExportResVO;
import com.tiancheng.trade.order.vo.admin.res.BillQueryResVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: kellen
 * @Description: 账单管理Controller 平台端
 * @Date: create in 2024/12/26 20:21
 */
@Slf4j
@RestController
@RequestMapping("/admin/bill")
public class BillAdminController {

    @Resource
    private BillService billService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<BillQueryResVO>> queryPage(@RequestBody QueryBillReqVO params) {

        PageInfo<BillQueryResVO> pageInfo = billService.queryPage(params);
        return Result.success(pageInfo);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void export(@RequestBody QueryBillReqVO params, HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置最多导出50000条
        params.setCurrentPage(1);
        params.setPageSize(50000);
        PageInfo<BillQueryResVO> pageInfo = billService.queryPage(params);
        Asserts.isTrue(pageInfo.getPagination().getTotal()<=50000,"最多支持导出50000条，请使用搜索条件减少导出数量！");
        if(pageInfo.getPagination().getTotal()>0){
            final List<BillQueryExportResVO> res = pageInfo.getList().parallelStream().map(o -> {
                final BillQueryExportResVO billQueryExportRes = new BillQueryExportResVO();
                billQueryExportRes.setOrderId(o.getOrderId());
                billQueryExportRes.setMerchant(o.getMerchantName()+"("+o.getMerchantCode()+")");
                billQueryExportRes.setCreatedDt(DateTimeUtil.format(o.getCreatedDt(), DateTimeUtil.bj_pattern));
                billQueryExportRes.setTotalAmount(AmountUtils.amount2Yuan(o.getTotalAmount()));
                billQueryExportRes.setDiscount(AmountUtils.amount2Yuan(o.getDiscount()));
                billQueryExportRes.setPayAmount(AmountUtils.amount2Yuan(o.getPayAmount()));
                billQueryExportRes.setRefundTotalAmount(AmountUtils.amount2Yuan(o.getRefundTotalAmount()));
                billQueryExportRes.setRefundDiscountAmount(AmountUtils.amount2Yuan(o.getRefundDiscountAmount()));
                billQueryExportRes.setRefundAmount(AmountUtils.amount2Yuan(o.getRefundAmount()));
                billQueryExportRes.setSettleStatus(AmountUtils.amount2Yuan(o.getSettleAmount()));
                if (null != o.getPlatformPercent()) {
                    billQueryExportRes.setPlatformPercent(o.getPlatformPercent().toString() + "%");
                } else {
                    billQueryExportRes.setPlatformPercent("0");
                }
                billQueryExportRes.setPlatformAmount(AmountUtils.amount2Yuan(o.getPlatformAmount()));
                billQueryExportRes.setMerchantAmount(AmountUtils.amount2Yuan(o.getMerchantAmount()));
                billQueryExportRes.setSettleStatus(SettleStatusEnum.getDesc(o.getSettleStatus()));
                return billQueryExportRes;
            }).collect(Collectors.toList());
            String fileName = "账单列表导出.xlsx";
        }
    }

    /**
     * 商户交易统计
     * @param platformMerchantId
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ResponseBody
        public Result<TradeStatisticsResVO> tradeStatistics(@RequestParam(name = "platformMerchantId",required = false) Long platformMerchantId) {
        //TradeStatisticsResVO result = billService.tradeStatistics(platformMerchantId);
        return Result.success(null);
    }



}
